package com.pcbly.web.components.utils.template;

import java.io.File;
import java.io.IOException; 

import com.pcbly.web.bean.table.ColumnPropertyBean; 

import org.apache.commons.io.FileUtils;

/**
 * 模块生成器
 * 
 * <p>
 * 用于生成JEE模块。
 * <p>
 * 包括JAVA类：action,dao,dao.impl,manager,manager.impl；
 * 配置文件：action配置,spring配置；ftl页面：list.html,add.html,edit.html。
 * 数据验证文件：Act-Com_save
 * -validation.xml,Act-Com_edit-validation.xml,Act-Com_update-validation.xml
 * <p>
 * 可设置的参数有：模块实体类名、java类包地址、配置文件地址、ftl页面地址。
 * 
 * 
 */
@SuppressWarnings("unchecked")
public final class ModuleGenerator {
 
	final static String[] fileName = { "action.txt", "xml_struts_config.txt",
			"applicationContext_dao.txt", "applicationContext_service.txt",
			"applicationContext_struts.txt", "dao_impl.txt", "dao.txt",
			"ServiceImp.txt", "Service.txt" };

	private Class clz;

	private String entity;

	private String entityP;

	private String entityL;
	
	private String firstEntityL;

	private String clzName;

	private String tableName;

	public String getEntityP() {
		return entityP;
	}

	public String getFirstEntityL() {
		return firstEntityL;
	}

	public void setFirstEntityL(String firstEntityL) {
		this.firstEntityL = firstEntityL;
	}

	public void setEntityP(String entityP) {
		this.entityP = entityP;
	}

	public String getEntityL() {
		return entityL;
	}

	public void setEntityL(String entityL) {
		this.entityL = entityL;
	}

	public String getClzName() {
		return clzName;
	}

	public void setClzName(String clzName) {
		this.clzName = clzName;
	}

	public String getTableName() {
		return tableName;
	}

	public void setTableName(String tableName) {
		this.tableName = tableName;
	}

	public Class getClz() {
		return clz;
	}

	public void setClz(Class clz) {
		this.clz = clz;
	}

	public String getEntity() {
		return entity;
	}

	public void setEntity(String entity) {
		this.entity = entity;
	}

	public ModuleGenerator(Class clz) {
		this.clz = clz;
		this.clzName = clz.getName();
//		System.out.println(this.clzName);
		this.entity = this.clzName.substring(this.clzName.lastIndexOf(".") + 1,
				this.clzName.length());
//		System.out.println(this.entity);
		String[] args = this.clzName.split("\\.");
		this.entityL = args[args.length - 2];
//		System.out.println(this.entityL);
		this.entityP = this.entity.substring(0,this.entity.length()-4);
//		System.out.println(this.entityP);
		this.firstEntityL=this.entityP.replace(this.entityP.substring(0,1), this.entityP.substring(0,1).toLowerCase());
//		System.out.println(this.firstEntityL);
	}

	public void execute() {
		try {
			
			String pathXml=actionXmlPath();
			System.out.println("<import resource=\"" + pathXml + "\"/>");
			File file = new File(pathXml);
			// System.out.println(file.getAbsolutePath());
			/************** applicationContext_struts.txt **************************/ 
			String name = "applicationContext_struts.txt";
			StringBuilder sb = new StringBuilder();
			pathXml=getFilePath(Constant.packName, name);
			
			File fileXML = new File(pathXml);
			sb.append(readTpl(fileXML));
			FileUtils.writeStringToFile(file, sb.toString(), "UTF-8");
			sb.delete(0, sb.length());

			/************** struts.txt **************************/
			pathXml=actionStrutsPath();
			System.out.println("<import resource=\"" + pathXml + "\"/>");
			file = new File(pathXml);
			name = "struts.txt";
			fileXML = new File(getFilePath(Constant.packName, name));
			sb.append(readTpl(fileXML));
			FileUtils.writeStringToFile(file, sb.toString(), "UTF-8");
			
			sb.delete(0, sb.length());

			/************** action.txt **************************/
			file = new File(actionPath());
			name = "action.txt";
			fileXML = new File(getFilePath(Constant.packName, name));
			sb.append(readTpl(fileXML));
			FileUtils.writeStringToFile(file, sb.toString(), "UTF-8");
			sb.delete(0, sb.length());

			/************** applicationContext_dao.txt **************************/
			pathXml=daoXmlPath();
			System.out.println("<import resource=\"" + pathXml + "\"/>");
			file = new File(pathXml);
			name = "applicationContext_dao.txt";
			fileXML = new File(getFilePath(Constant.packName, name));
			sb.append(readTpl(fileXML));
			FileUtils.writeStringToFile(file, sb.toString(), "UTF-8");
			sb.delete(0, sb.length());

			/************** dao_impl.txt **************************/
			file = new File(daoImplPath());
			name = "dao_impl.txt";
			fileXML = new File(getFilePath(Constant.packName, name));
			sb.append(readTpl(fileXML));
			FileUtils.writeStringToFile(file, sb.toString(), "UTF-8");
			sb.delete(0, sb.length());

			/************** dao.txt **************************/
			file = new File(daoPath());
			name = "dao.txt";
			fileXML = new File(getFilePath(Constant.packName, name));
			sb.append(readTpl(fileXML));
			FileUtils.writeStringToFile(file, sb.toString(), "UTF-8");
			sb.delete(0, sb.length());

			/************** Service.txt **************************/
			file = new File(servicePath());
			name = "Service.txt";
			fileXML = new File(getFilePath(Constant.packName, name));
			sb.append(readTpl(fileXML));
			FileUtils.writeStringToFile(file, sb.toString(), "UTF-8");
			sb.delete(0, sb.length());

			/************** Service.txt **************************/
			pathXml=serviceXmlPath();
			System.out.println("<import resource=\"" + pathXml + "\"/>");
			file = new File(pathXml);
			name = "applicationContext_service.txt";
			fileXML = new File(getFilePath(Constant.packName, name));
			sb.append(readTpl(fileXML));
			FileUtils.writeStringToFile(file, sb.toString(), "UTF-8");
			sb.delete(0, sb.length());

			/************** Service.txt **************************/
			file = new File(serviceImpPath());
			name = "ServiceImp.txt";
			fileXML = new File(getFilePath(Constant.packName, name));
			sb.append(readTpl(fileXML));
			FileUtils.writeStringToFile(file, sb.toString(), "UTF-8");
			sb.delete(0, sb.length());

			if (file != null)
				file = null;

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	protected String actionXmlPath() {
		String packageName = "com.pcbly.web.action." + this.entityL;
		String name = this.firstEntityL+"-applicationContext-struts.xml";
		return getFilePath(packageName, name);
	}

	protected String actionStrutsPath() {
		String packageName = "com.pcbly.web.action." + this.entityL;
		String name = this.firstEntityL+"-struts.xml";
		return getFilePath(packageName, name);
	}

	protected String actionPath() {
		String packageName = "com.pcbly.web.action." + this.entityL;
		String name = this.entityP + "Action.java";
		return getFilePath(packageName, name);
	}

	protected String daoXmlPath() {
		String packageName = "com.pcbly.web.persistence." + this.entityL;
		String name = this.firstEntityL+"-applicationContext-dao.xml";
		return getFilePath(packageName, name);
	}

	protected String daoImplPath() {
		String packageName = "com.pcbly.web.persistence." + this.entityL;
		String name = this.entityP + "HibernateDao.java";
		return getFilePath(packageName, name);
	}

	protected String daoPath() {
		String packageName = "com.pcbly.web.persistence." + this.entityL;
		String name = this.entityP + "Dao.java";
		return getFilePath(packageName, name);
	}

	protected String servicePath() {
		String packageName = "com.pcbly.web.business." + this.entityL;
		String name = this.entityP + "Service.java";
		return getFilePath(packageName, name);
	}

	protected String serviceXmlPath() {
		String packageName = "com.pcbly.web.business." + this.entityL;
		String name = this.firstEntityL+"-applicationContext-service.xml";
		return getFilePath(packageName, name);
	}

	protected String serviceImpPath() {
		String packageName = "com.pcbly.web.business." + this.entityL;
		String name = this.entityP + "ServiceImp.java";
		return getFilePath(packageName, name);
	}

	protected String getFilePath(String packageName, String name) {
//		System.out.println("replace:" + packageName);
		String path = packageName.replaceAll("\\.", "/");
//		System.out.println("after relpace:" + path);
		return "src/" + path + "/" + name;
	}

	public void generate() {

	}

	public ModuleGenerator(Class clz, String tableName) {
		this.clz = clz;
		this.tableName = tableName;

	}

	String readTpl(File tpl) {
		return readTpl(tpl, "UTF-8");
	}

	String readTpl(File tpl, String charset) {
		String content = null;
		try {
			content = FileUtils.readFileToString(tpl, charset);
			content = content.replaceAll("\\#\\{entityL\\}", this.entityL);
			content = content.replaceAll("\\#\\{entityP\\}", this.entityP);
			content = content.replaceAll("\\#\\{clzName\\}", this.clzName);
			content = content.replaceAll("\\#\\{entity\\}", this.entity);
			content = content.replaceAll("\\#\\{tableName\\}", this.tableName);
			content = content.replaceAll("\\#\\{firstEntityL\\}", this.firstEntityL);
			
		} catch (IOException e) {
			System.out.println("read file faild. " + e.getMessage());
		}
		return content;
	}

	public static void main(String[] args) {
		ModuleGenerator mg = new ModuleGenerator(ColumnPropertyBean.class);
		mg.setTableName("t_table_column_property"); 
		mg.execute();
	}
}
